package com.newland.zxy.config;

import com.newland.zxy.service.My403Service;
import com.newland.zxy.service.MyAccessDenyHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author : zhangxuyuan
 * @Project: springsecurity
 * @Package com.newland.zxy.config
 * @date Date : 2021年02月08日 10:34
 */
@Configuration
//打开权限注解的支持
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {

    //认证控制器
    @Override
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }


    @Bean
    @Override
    public UserDetailsService userDetailsService(){
        //把用户信息存储在内存里，这里是为了代码调试方便将用户信息存在内存中
        //正常情况下是从数据库读取用户数据，见自定义实现的MyUserDetailsService
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.withUsername("zhangsan").password("123").authorities("user").build());
        manager.createUser(User.withUsername("lisi").password("456").authorities("user").build());
        return manager;
    }

    //密码加密编码器，下面这个是编码器不对密码进行编码
    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }

    //主要相关配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //需要认证的url
                .antMatchers("/test/**")
                .authenticated()
                //其他url放行，不需要认证
                .anyRequest().permitAll()
                .and()
                .exceptionHandling()
                //url用户无权限时处理的handler
                .accessDeniedHandler(accessDeniedHandler())
                //用户认证未通过时处理的handler
                .authenticationEntryPoint(authenticationEntryPoint())
                .and()
                .csrf().disable()
                //设置自定义的过滤器
                .addFilterBefore(new JwtSecurityFilter(super.authenticationManager())
                        ,UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    public AccessDeniedHandler accessDeniedHandler()
    {
        return new MyAccessDenyHandler();
    }

    @Bean
    public AuthenticationEntryPoint authenticationEntryPoint()
    {
        return new My403Service();
    }
}
